package com.shoushuzhitongche.app.view.localdata.server;

import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.config.SystemConfig;
import com.dixiang.framework.network.Net;
import com.google.gson.reflect.TypeToken;
import com.shoushuzhitongche.app.utls.CommonUtils;
import com.shoushuzhitongche.app.utls.Constants;
import com.shoushuzhitongche.app.view.localdata.bean.LocalDataEntity;
import com.shoushuzhitongche.app.view.localdata.dao.LocalDataDao;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class DataCheckService extends Service{

	private Context mContext;
	public final static int GET_CITY = 1;
	public final static int GET_LOCAL_DATA = 2;
	public final static int SUCCESS = 3;
	public String localUrl = "";
	private int sum = 0;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case SUCCESS:
				CommonUtils.saveLocalData(mContext, true);
				stopSelf();
				break;	
			default:
				break;
			}
		}
		
	};
	
	@Override
	public void onCreate() {
		super.onCreate();
		mContext = this;
		
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	private void getLocalData(){

		String actionUrl = localUrl + "?api="+SystemConfig.API;
		Net.fetch(getApplicationContext(),
				actionUrl, "",
				new TypeToken<QjResult<LocalDataEntity>>() {
				}, new QJNetUICallback<QjResult<LocalDataEntity>>(mContext){
					@Override
					public void onSuccess(QjResult<LocalDataEntity> result ,int flg) {
						super.onSuccess(result,flg);
						if(result != null && result.getResults() != null){
							LocalDataEntity localDataEntity = result.getResults();
							if(localDataEntity != null ){
								LocalDataDao.newInstance(mContext).saveLocalData(localDataEntity,handler);
							}
						} 
					}
					
					@Override
					public void onError(Exception e, QjResult<LocalDataEntity> result,
							String requestFlag) {
						super.onError(e, result);
							if(sum == 2){
								stopSelf();
							}else {
								sum ++;
								getLocalData();
							}
					}
					
					@Override
					public void onFailure(QjResult<LocalDataEntity> result,
							int flg) {
						super.onFailure(result, flg);
						if(sum == 2){
							stopSelf();
						}else {
							sum ++;
							getLocalData();
						}
					}
					
					@Override
					public void onCompleted(Exception e, QjResult<LocalDataEntity> result) {
						super.onCompleted(e, result);
					}
				});
	}
	
	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		if(intent != null)
			localUrl = intent.getStringExtra(Constants.PARMS.ACTIONURL);
		Log.e("====", "service onstart");
		getLocalData();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.e("====", "service onDestroy");
	}
	
	
}
